package com.example.wac.config;

import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

@Configuration
public class DruidMonitroConfiguration {

    @Bean("druidStatViewServlet")
    public ServletRegistrationBean<StatViewServlet> getDruidStatViewServlet() {
        ServletRegistrationBean<StatViewServlet> registrationBean
                = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");

        // 白名单
        registrationBean.addInitParameter(StatViewServlet.PARAM_NAME_ALLOW, "127.0.0.1");
        // 黑名单
        registrationBean.addInitParameter(StatViewServlet.PARAM_NAME_DENY, "");
        // 用户名
        registrationBean.addInitParameter(StatViewServlet.PARAM_NAME_USERNAME, "admin");
        // 密码
        registrationBean.addInitParameter(StatViewServlet.PARAM_NAME_PASSWORD, "admin");
        // 允许重置
        registrationBean.addInitParameter(StatViewServlet.PARAM_NAME_RESET_ENABLE, "true");
        return registrationBean;
    }

    @Bean("webStatFilter")
    public WebStatFilter getWebStatFilter() {
        WebStatFilter statFilter = new WebStatFilter();
        // 对session进行监控
        statFilter.setSessionStatEnable(true);
        return statFilter;
    }

    /**
     * @param webStatFilter
     * @return
     */
    @Bean
    @DependsOn("webStatFilter")
    public FilterRegistrationBean<WebStatFilter> getDruidStatViewServlet(WebStatFilter webStatFilter) {
        FilterRegistrationBean<WebStatFilter> registrationBean
                = new FilterRegistrationBean<>(webStatFilter);
        // 对所有的路径都进行监控配置
        registrationBean.addUrlPatterns("/*");
        // 排除路径
        registrationBean.addInitParameter
                (WebStatFilter.PARAM_NAME_EXCLUSIONS, "*.js,*.gif,/druid/*");
        return registrationBean;


    }

}